import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop) 

        toolBar =QToolBar()

        cb = QCheckBox('IsReadOnly')
        cb.stateChanged.connect(self.OnIsReadOnlyChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('ClearButtonEnable')
        cb.stateChanged.connect(self.OnClearButtonEnableChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Alignment:'))
        alignment = QComboBox()        
        alignment.addItems(Qt.AlignmentFlag.__members__.keys())
        alignment.currentTextChanged.connect(self.AlignmentChanged)
        toolBar.addWidget(alignment)        

        toolBar.addWidget(QLabel('EchoMode:'))
        EchoMode = QComboBox()
        EchoMode.addItems(QLineEdit.EchoMode.__members__.keys())
        EchoMode.currentTextChanged.connect(self.EchoModeChanged)
        toolBar.addWidget(EchoMode)        

        toolBar.addWidget(QLabel('PlaceholderText:'))
        self.placeholder = QLineEdit()
        self.placeholder.editingFinished.connect(self.SetPlaceholderText)
        toolBar.addWidget(self.placeholder)

        toolBar.addWidget(QLabel('InputMask:'))
        self.inputMask = QLineEdit()
        self.inputMask.editingFinished.connect(self.SetInputMask)
        toolBar.addWidget(self.inputMask)

        toolBar.addWidget(QLabel('MaxLength:'))
        self.maxlength = QLineEdit()
        intValidator=QIntValidator()
        intValidator.setRange(1,65536)
        self.maxlength.setValidator(intValidator)
        self.maxlength.editingFinished.connect(self.SetMaxLength)
        toolBar.addWidget(self.maxlength)

        toolBar1=QToolBar()      
        btn = QPushButton('Add Action')
        btn.clicked.connect(self.AddAction)
        toolBar1.addWidget(btn)

        btn = QPushButton('Remove Cursor')
        btn.setToolTip('Move one cursor to the right')
        btn.clicked.connect(self.RemoveCursor)
        toolBar1.addWidget(btn)

        layout.addWidget(toolBar)
        layout.addWidget(toolBar1)

        self.lineEdit = QLineEdit()
        layout.addWidget(self.lineEdit)

        group=QGroupBox('LineEdit with IntValidator(10~100)')
        groupLayout=QVBoxLayout()
        edit=QLineEdit()
        intValidator=QIntValidator()
        intValidator.setRange(10,100)              
        edit.setValidator(intValidator)
        groupLayout.addWidget(edit)
        group.setLayout(groupLayout)
        layout.addWidget(group)

        group=QGroupBox('LineEdit with DoubleValidator(10~50)')
        groupLayout=QVBoxLayout()
        edit=QLineEdit()
        doubleValidator=QDoubleValidator()
        doubleValidator.setRange(10,50)
        doubleValidator.setDecimals(2)       
        edit.setValidator(doubleValidator)
        groupLayout.addWidget(edit)
        group.setLayout(groupLayout)
        layout.addWidget(group)

        group=QGroupBox('LineEdit with RegularValidator')
        groupLayout=QVBoxLayout()
        edit=QLineEdit()
        validator=QRegularExpressionValidator()
        validator.setRegularExpression(QRegularExpression('[a-zA-Z0-9]+$'))               
        edit.setValidator(validator)
        groupLayout.addWidget(edit)
        group.setLayout(groupLayout)
        layout.addWidget(group)

        self.setLayout(layout) 
    def OnClearButtonEnableChanged(self,state):
        self.lineEdit.setClearButtonEnabled(state)
        self.parent.OnShowMessage(f"Set ClearButtonEnabled={state}")
    def OnIsReadOnlyChanged(self,state):
        self.lineEdit.setReadOnly(state)
        self.parent.OnShowMessage(f"Set ReadOnly={state}")
    def AlignmentChanged(self,text):
        alignment = Qt.AlignmentFlag[text]
        self.lineEdit.setAlignment(alignment)
        self.parent.OnShowMessage(f'Set Alignment: {alignment}')
    def EchoModeChanged(self,text):
        mode = QLineEdit.EchoMode[text]
        self.lineEdit.setEchoMode(mode)
        self.parent.OnShowMessage(f'Set EchoMode: {mode}')
    def SetPlaceholderText(self):
        placeholder = self.placeholder.text()
        self.lineEdit.setPlaceholderText(placeholder)
        self.parent.OnShowMessage(f'Set PlaceholderText: {placeholder}')
    def SetInputMask(self):
        inputMask = self.inputMask.text()
        self.lineEdit.setInputMask(inputMask)
        self.parent.OnShowMessage(f'Set InputMask: {inputMask}')
    def SetMaxLength(self):
        length=int(self.maxlength.text())
        self.lineEdit.setMaxLength(length)
        self.parent.OnShowMessage(f'Set MaxLength: {length}')
    def AddAction(self):        
        action = QAction(f'Action{len(self.lineEdit.actions())}',self.lineEdit)
        action.setIcon(QIcon(f'{self.parent.getAppDir()}/resources/confirm.png'))
        self.lineEdit.addAction(action,QLineEdit.ActionPosition.TrailingPosition)
        self.parent.OnShowMessage(f'Add Action: {action.text()}')
    def RemoveCursor(self):
        self.lineEdit.cursorForward(True,1)
        self.parent.OnShowMessage(f'Current Cursor: {self.cursorPosition()}')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet